package com.next

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
@Transactional(readOnly = true)
class FollowController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def springSecurityService

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond Follow.list(params), model: [followCount: Follow.count()]
    }

    def show(Follow follow)
    {
        respond follow
    }

    def create()
    {
        respond new Follow(params)
    }

    @Transactional
    def save(Follow follow)
    {
        if (follow == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (follow.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond follow.errors, view: 'create'
            return
        }

        follow.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'follow.label', default: 'Follow'), follow.id])
                redirect follow
            }
            '*' { respond follow, [status: CREATED] }
        }
    }

    def edit(Follow follow)
    {
        respond follow
    }

    @Transactional
    def update(Follow follow)
    {
        if (follow == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (follow.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond follow.errors, view: 'edit'
            return
        }

        follow.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'follow.label', default: 'Follow'), follow.id])
                redirect follow
            }
            '*' { respond follow, [status: OK] }
        }
    }

    @Transactional
    def delete(Follow follow)
    {

        if (follow == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        follow.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'follow.label', default: 'Follow'), follow.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'follow.label', default: 'Follow'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    @Transactional
    def addFollow()
    {
        def username = springSecurityService?.getPrincipal()?.username
        def user = User.findByUsername(username)
        def follow = new Follow()
        follow.user = user

        def followObject = params['followObject']
        def objectId = params['objectId']
        def object

        switch (followObject)
        {
        case "account":
            object = Account.findById(objectId)
            follow.account = object
            follow.source = object?.user
            break
        case "contact":
            object = Contact.findById(objectId)
            follow.contact = object
            follow.source = object?.user
            break
        case "opportunity":
            object = Opportunity.findById(objectId)
            follow.opportunity = object
            follow.source = object?.user
            break
        case "activity":
            object = Activity.findById(objectId)
            follow.activity = object
            follow.source = object?.user
            break
        case "issue":
            object = Issue.findById(objectId)
            follow.issue = object
            follow.source = object?.user
            break
        case "asset":
            object = Asset.findById(objectId)
            follow.asset = object
            follow.source = object?.user
            break
        case "campaign":
            object = Campaign.findById(objectId)
            follow.campaign = object
            follow.source = object?.user
            break
        }

        follow.save()
        redirect uri: request.getHeader("referer")
    }

    @Transactional
    def cancelFollow(Follow follow)
    {
        follow.delete flush: true
        redirect uri: request.getHeader("referer")
    }
}
